package main

import (
	"context"
	"log"
	"time"

	pb "github.com/plutov/packagemain/grpc-invoke-java/gen" // 替换为实际的路径
	"google.golang.org/grpc"
)

func main() {
	// 连接到 Java gRPC 服务器
	conn, err := grpc.Dial("192.168.65.130:9090", grpc.WithInsecure())
	if err != nil {
		log.Fatalf("Did not connect: %v", err)
	}
	defer conn.Close()

	client := pb.NewUserServiceClient(conn)

	// 创建请求
	req := &pb.UserRequest{Id: 1}

	// 设置上下文超时
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	// 调用 GetUser 方法
	res, err := client.GetUser(ctx, req)
	if err != nil {
		log.Fatalf("Could not get user: %v", err)
	}

	log.Printf("User Name: %s", res.GetName())
	log.Printf("User Email: %s", res.GetEmail())
}
